{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           20010827  20010828  20010829  20010830  20010831  20010903  \\\n",
      "600519.SH     34.51     34.99     36.98     36.28     37.15      37.2   \n",
      "\n",
      "           20010904  20010905  20010906  20010907  ...  20240422  20240423  \\\n",
      "600519.SH     37.01     37.61     37.35      36.5  ...    1654.0    1675.0   \n",
      "\n",
      "           20240424  20240425  20240426  20240429  20240430  20240506  \\\n",
      "600519.SH   1696.98   1690.33    1694.0   1696.95   1705.01   1733.97   \n",
      "\n",
      "           20240507  20240508  \n",
      "600519.SH   1765.79   1769.69  \n",
      "\n",
      "[1 rows x 5504 columns]\n"
     ]
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "xtdata.download_history_data2(stock_list=['600519.SH'], period='1d')\n",
    "res = xtdata.get_market_data(stock_list=['600519.SH'], period='1d')\n",
    "print(res['open'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 交易下单"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "连接成功\n",
      "1082141301\n",
      "1082141300\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "from xtquant.xttype import StockAccount\n",
    "from xtquant.xttrader import XtQuantTrader\n",
    "from xtquant import xtconstant\n",
    "\n",
    "# miniQMT安装路径\n",
    "mini_qmt_path = r'D:\\e海方舟-量化交易版\\userdata_mini'\n",
    "\n",
    "# QMT账号\n",
    "account = '10503139'\n",
    "# 创建session_id\n",
    "session_id = int(random.randint(100000, 999999))\n",
    "# 创建交易对象\n",
    "xt_trader = XtQuantTrader(mini_qmt_path, session_id)\n",
    "\n",
    "# 启动交易对象\n",
    "xt_trader.start()\n",
    "# 连接客户端\n",
    "connect_result = xt_trader.connect()\n",
    "\n",
    "if connect_result == 0:\n",
    "    print('连接成功')\n",
    "else:\n",
    "    print('连接失败')\n",
    "\n",
    "# 创建账号对象\n",
    "acc = StockAccount(account)\n",
    "# 订阅账号\n",
    "xt_trader.subscribe(acc)\n",
    "# 下单\n",
    "res = xt_trader.order_stock(acc, stock_code='600971.SH', order_type=xtconstant.STOCK_BUY, order_volume=100, price_type=xtconstant.LATEST_PRICE, price=-1)\n",
    "order_id = xt_trader.order_stock(acc, stock_code='600000.SH', order_type=xtconstant.STOCK_BUY, order_volume=100,\n",
    "                                  price_type=xtconstant.MARKET_PEER_PRICE_FIRST, price=xtconstant.LATEST_PRICE,\n",
    "                                    strategy_name='strategy1', order_remark='order_test')\n",
    "\n",
    "print(order_id)\n",
    "\n",
    "print(res)\n",
    "\n",
    "# xt_trader.run_forever()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "miniqmt-demo-cptEsqYo-py3.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
